package com.demo.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.demo.cms.dao.ChannelDao;
import com.demo.cms.model.Channel;
import com.demo.cms.service.ChannelService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;

/**
 * 频道服务实现类
 * @author ryuu.kk
 *
 */
public class ChannelServiceImpl implements ChannelService {

	/**
	 * 频道数据访问层
	 */
	private ChannelDao channelDao;
	
	@Override
	public Long save(Channel channel) {
		return channelDao.insert(channel);
	}

	@Override
	public boolean exists(Long id) {
		return channelDao.exists(id);
	}

	@Override
	public void update(Long id, String title, String path, Long templateId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		Channel channel = new Channel();
		channel.setTitle(title);
		channel.setTemplateId(templateId);
		channel.setI18n(i18n);
		channel.setPath(path);
		channelDao.update(channel, params);
	}

	@Override
	public void update(Long id, boolean enabled, String status) {
		// 更新条件
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		// 更新数据
		Channel channel = new Channel();
		channel.setEnabled(enabled);
		channel.setStatus(status);
		// 更新
		channelDao.updateEnabled(channel, params);
	}
	
	@Override
	public void update(Long id, String status) {
		// 更新条件
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		// 更新
		channelDao.updateStatus(status, params);
	}
	
	@Override
	public void updateAll(Long siteId, String status) {
		// 更新条件
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		// 更新
		channelDao.updateStatus(status, params);
	}

	@Override
	public void delete(Long id) {
		channelDao.delete(id);
	}

	@Override
	public void deleteAll(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		channelDao.delete(params);
	}
	
	@Override
	public Channel find(Long id) {
		return channelDao.select(id);
	}
	
	@Override
	public Channel find(String path) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("path", path);
		List<Channel> channelList = channelDao.select(params);
		return channelList.size() > 0 ? channelList.get(0) : null;
	}
	
	@Override
	public List<Channel> findList(Long siteId) {
		return findList(siteId, null, "");
	}

	@Override
	public List<Channel> findList(Long siteId, String i18n) {
		return findList(siteId, null, i18n);
	}

	@Override
	public List<Channel> findList(Long siteId, Long templateId) {
		return findList(siteId, templateId, "");
	}

	@Override
	public List<Channel> findList(Long siteId, Long templateId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("templateId", templateId);
		return channelDao.select(params);
	}
	
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}

	@Override
	public ResultSearchResult<Channel> search(Long siteId, String i18n, int firstResult, int maxResults) {
		Map<String, Object> params = new HashMap<String, Object>(7);
		params.put("siteId", siteId);
		params.put("i18n", i18n);
		SearchArgumentSupport searchSupport = new SearchArgumentSupport(firstResult, maxResults);
		return channelDao.search(params, searchSupport);
	}

	@Override
	public ResultSearchResult<Channel> search(Long siteId, int firstResult, int maxResults) {
		return search(siteId, null, firstResult, maxResults);
	}

	@Override
	@Transactional
	public void setDefault(Long id, Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		channelDao.updateStatus(Channel.STATUS_NORMAL, params);
		params.put("id", id);
		channelDao.updateStatus(Channel.STATUS_DEFAULT, params);
	}

	@Override
	public Channel findDefault(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		params.put("status", Channel.STATUS_DEFAULT);
		
		List<Channel> channelList = channelDao.select(params);
		if (channelList.size() > 0) {
			return channelList.get(0);
		}
		// 得到第一个频道,作为默认频道使用
		params.clear();
		params.put("siteId", siteId);
		channelList = channelDao.select(params);
		
		return channelList.size() > 0 ? channelList.get(0) : null;
	}
}
